package com.duolaibei.business.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.duolaibei.business.R;
import com.duolaibei.business.api.ApiName;
import com.duolaibei.business.base.BaseActivity;
import com.duolaibei.business.config.Preferences;
import com.duolaibei.business.http.HttpUtils;
import com.duolaibei.business.model.ResponseResult;
import com.duolaibei.business.utils.CommonUtil;
import com.duolaibei.business.utils.GsonUtil;
import com.duolaibei.business.utils.PreferenceUtil;
import com.duolaibei.business.utils.cutdown.CountDownTimerUtil;
import com.duolaibei.business.utils.inject.Inject;
import com.duolaibei.business.view.dialog.WorkerAddTypeDialog;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by xlm on 2017/11/10 0010
 */

public class WorkerAddActivity extends BaseActivity {

    @Inject(R.id.back)
    private RelativeLayout mLayoutBack;
    @Inject(R.id.title)
    private TextView mViewTitle;

    private EditText mEditTextType;//类型
    private EditText mEditTextWorkUid;//员工UID
    private EditText mEditTextMobile;//手机号
    private EditText mEditTextYzCode;//验证码
    private EditText mEditTextPassword;//密码
    private EditText mEditTextConfitmPassword;//确认密码

    @Inject(R.id.addworker_getyzcode)
    private TextView mTextViewGetYzCode;//获取验证码
    @Inject(R.id.confirm_btn)
    private TextView mTextViewConfirm;//确定

    @Inject(R.id.layout_username)
    private LinearLayout mLayoutUsername;//添加UID
    @Inject(R.id.layout_regist)
    private LinearLayout mLayoutRegist;//注册并添加


    private String worker_uid;//员工uid

    private String type;//(username regist)

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_worker_add);
    }

    @Override
    protected void initViews() {
        mEditTextType= (EditText) findViewById(R.id.addworker_type);
        mEditTextWorkUid= (EditText) findViewById(R.id.addworker_uid);
        mEditTextMobile= (EditText) findViewById(R.id.addworker_mobile);
        mEditTextYzCode= (EditText) findViewById(R.id.addworker_yzcode);
        mEditTextPassword= (EditText) findViewById(R.id.addworker_password);
        mEditTextConfitmPassword= (EditText) findViewById(R.id.addworker_password_comfirm);
        mViewTitle.setText("新增员工");
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {

    }


    @Override
    protected void onSetListener() {
        mLayoutBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mEditTextType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showWorkerAddDialog();
            }
        });
        mTextViewGetYzCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSmsData();
            }
        });
        mTextViewConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type != null) {
                    if ("username".equals(type)) {
                        worker_uid = mEditTextWorkUid.getText().toString();
                        if (CommonUtil.isEmpty(worker_uid)) {
                            showToast("请输入员工UID");
                            return;
                        }
                        addWorkerUid(worker_uid);
                    } else if ("regist".equals(type)) {
                        getRegistData();
                    }
                }
            }
        });
    }

    private WorkerAddTypeDialog mWorkerAddTypeDialog;

    private void showWorkerAddDialog() {
        if (mWorkerAddTypeDialog == null) {
            mWorkerAddTypeDialog = new WorkerAddTypeDialog(getThis());
        }
        mWorkerAddTypeDialog.setCanceledOnTouchOutside(true);
        mWorkerAddTypeDialog.setListener(new WorkerAddTypeDialog.Listener() {
            @Override
            public void username() {
                super.username();
                type = "username";
                mEditTextType.setText("添加员工账号");
                mLayoutUsername.setVisibility(View.VISIBLE);
                mLayoutRegist.setVisibility(View.GONE);
                mWorkerAddTypeDialog.dismiss();
            }

            @Override
            public void regist() {
                super.regist();
                type = "regist";
                mEditTextType.setText("注册员工账号并添加");
                mLayoutUsername.setVisibility(View.GONE);
                mLayoutRegist.setVisibility(View.VISIBLE);
                mWorkerAddTypeDialog.dismiss();
            }
        });
        mWorkerAddTypeDialog.show();
    }

    /**
     * 添加员工
     */
    private void addWorkerUid(String worker_uid) {
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", ApiName.WORKER_ADD);
        param.add("uid", String.valueOf(PreferenceUtil.getParam(Preferences.UID, "")));
        param.add("login_type", "merchant");
        param.add("workmen_uid", worker_uid);
        param.add("sign", param.getMd5String());
        showLoading("添加中");
        HttpUtils.getInstance().url(ApiName.API)
                .params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                dismissLoading();
                if (CommonUtil.isForeground(getThis())) {
                    showToast(getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                dismissLoading();
                if (HttpUtils.isJson(string)) {
                    ResponseResult result = GsonUtil
                            .GsonToBean(string, ResponseResult.class);
                    showToast(result.getInfo());
                    if (result.getStatus() == 1) {
                        Intent intent = new Intent();
                        intent.putExtra("worker_uid", "");
                        intent.putExtra("type", "all");
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                } else {
                    showToast(string);
                }
            }
        });
    }

    /**
     * 获取注册信息
     */
    private void getRegistData() {
        String mobile = mEditTextMobile.getText().toString();
        String yzcode = mEditTextYzCode.getText().toString();
        String password = mEditTextPassword.getText().toString();
        String confirm = mEditTextConfitmPassword.getText().toString();
        if (!CommonUtil.isMobile(mobile)) {
            showToast("请输入手机号码");
            return;
        }
        if (CommonUtil.isEmpty(yzcode)) {
            showToast("请输入验证码");
            return;
        }
        if (CommonUtil.isEmpty(password)) {
            showToast("请输入密码");
            return;
        }
        if (CommonUtil.isEmpty(confirm)) {
            showToast("请再次输入密码");
            return;
        }
        if (!password.equals(confirm)) {
            showToast("密码输入不一致");
            return;
        }
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", ApiName.REGISTER);
        param.add("reg_type", "mobile");
        param.add("tg", "0");
        param.add("ag", "40");
        param.add("user_type", "2");
        param.add("mobile", mobile);
        param.add("verify", yzcode);
        param.add("password", password);
        param.add("sign", param.getMd5String());
        showLoading("注册中");
        HttpUtils.getInstance().url(ApiName.API)
                .params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                dismissLoading();
                if (CommonUtil.isForeground(getThis())) {
                    showToast(getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                dismissLoading();
                if (HttpUtils.isJson(string)) {
                    ResponseResult result = GsonUtil.GsonToBean(string, ResponseResult.class);
                    showToast(result.getInfo());
                    if (result.getStatus() == 1) {
                        try {
                            //注册成功获取员工uid
                            worker_uid = new JSONObject(string)
                                    .getJSONObject("data").optString("id");
                            if (!CommonUtil.isEmpty(worker_uid)) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        addWorkerUid(worker_uid);
                                    }
                                });
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    showToast(string);
                }
            }
        });

    }


    /**
     * 获取短信验证码
     */
    private void getSmsData() {
        String mobile = mEditTextMobile.getText().toString();
        if (!CommonUtil.isMobile(mobile)) {
            showToast("请输入正确手机号码");
            return;
        }
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", ApiName.SENDSMS);
        param.add("mobile", mobile);
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().url(ApiName.API)
                .params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                if (HttpUtils.isJson(string)) {
                    try {
                        showToast(new JSONObject(string).optString("info"));
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                new CountDownTimerUtil(mTextViewGetYzCode,
                                        30000, 1000).start();
                            }
                        });
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    showToast(string);
                }
            }
        });
    }
}
